/* Project: POPS
 * CS 279, Vanderbilt University
 * by: Igor I. Ges, Michael S. Parrish, Mark E. Sakauye
 *
 *  This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.popsGUI.java;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.RepaintManager;

import com.popsDB.java.dto.Items;
import com.popsDB.java.dto.Pricesheetitems;
import com.popsDB.java.dto.Pricesheets;

//import com.popsDB.java.dao.CustomersDao;
//import com.popsDB.java.dao.ItemsDao;
//import com.popsDB.java.dao.OrdereditemsDao;
//import com.popsDB.java.dao.OrdersDao;
//import com.popsDB.java.factory.CustomersDaoFactory;
//import com.popsDB.java.factory.ItemsDaoFactory;
//import com.popsDB.java.factory.OrdereditemsDaoFactory;
//import com.popsDB.java.factory.OrdersDaoFactory;

/**
 *
 * @author  __USER__
 */
public class ItemPanel extends javax.swing.JPanel implements Printable {

	/** Creates new form ItemPanel */
	public ItemPanel() {
		initComponents();
		notificationDialog = new com.popsGUI.java.notificationDialog(null,false);
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		ip_find_item_dialog = new javax.swing.JDialog();
		ip_fid_itemID_field = new javax.swing.JTextField();
		ip_fid_find_button = new javax.swing.JButton();
		ip_fid_itemID_label = new javax.swing.JLabel();
		ip_fid_error_label = new javax.swing.JLabel();
		addToPST_dialog = new javax.swing.JDialog();
		jScrollPane1 = new javax.swing.JScrollPane();
		atpst_item_table = new javax.swing.JTable();
		atpst_save_button = new javax.swing.JButton();
		atpst_id_label = new javax.swing.JLabel();
		atpst_id_field = new javax.swing.JLabel();
		atpst_name_label = new javax.swing.JLabel();
		atpst_name_field = new javax.swing.JLabel();
		atpst_unit1_label = new javax.swing.JLabel();
		atpst_unit1_field = new javax.swing.JLabel();
		atpst_unit2_label = new javax.swing.JLabel();
		atpst_unit2_field = new javax.swing.JLabel();
		atpst_price1_field = new javax.swing.JLabel();
		atpst_price1_label = new javax.swing.JLabel();
		atpst_price2_label = new javax.swing.JLabel();
		atpst_price2_field = new javax.swing.JLabel();
		atpst_warning_label = new javax.swing.JLabel();
		atpst_counter_field = new javax.swing.JLabel();
		print_dialog = new javax.swing.JDialog();
		pd_printText_scrollpane = new javax.swing.JScrollPane();
		pd_printText_textarea = new javax.swing.JTextArea();
		jScrollPane7 = new javax.swing.JScrollPane();
		ip_itemList_table = new javax.swing.JTable();
		ip_itemList_label = new javax.swing.JLabel();
		ip_saveChanges_button = new javax.swing.JButton();
		ip_createNewItem_button = new javax.swing.JButton();
		ip_itemName_field = new javax.swing.JTextField();
		ip_description_field = new javax.swing.JTextField();
		ip_description_label = new javax.swing.JLabel();
		ip_unit1Price_label = new javax.swing.JLabel();
		ip_unit1Price_field = new javax.swing.JTextField();
		ip_unit1_field = new javax.swing.JTextField();
		ip_unit1_label = new javax.swing.JLabel();
		ip_unit2_label = new javax.swing.JLabel();
		ip_itemStatus_label = new javax.swing.JLabel();
		ip_unit2_field = new javax.swing.JTextField();
		ip_itemStatus_field = new javax.swing.JCheckBox();
		ip_itemName_label = new javax.swing.JLabel();
		ip_itemID_label = new javax.swing.JLabel();
		ip_itemID_field = new javax.swing.JTextField();
		ip_findItem_button = new javax.swing.JButton();
		ip_unit2Price_label = new javax.swing.JLabel();
		ip_unit2Price_field = new javax.swing.JTextField();
		ip_confirm_button = new javax.swing.JButton();
		ip_errorLabel = new javax.swing.JLabel();
		ip_progressBar = new javax.swing.JProgressBar();
		ip_progressBar_label = new javax.swing.JLabel();
		ip_reloadTable_button = new javax.swing.JButton();
		ip_print_button = new javax.swing.JButton();

		ip_find_item_dialog.setMinimumSize(new java.awt.Dimension(400, 400));

		ip_fid_itemID_field
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ip_fid_itemID_fieldActionPerformed(evt);
					}
				});

		ip_fid_find_button.setText("Find");
		ip_fid_find_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ip_fid_find_buttonActionPerformed(evt);
					}
				});

		ip_fid_itemID_label.setText("itemID");

		javax.swing.GroupLayout ip_find_item_dialogLayout = new javax.swing.GroupLayout(
				ip_find_item_dialog.getContentPane());
		ip_find_item_dialog.getContentPane().setLayout(
				ip_find_item_dialogLayout);
		ip_find_item_dialogLayout
				.setHorizontalGroup(ip_find_item_dialogLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								ip_find_item_dialogLayout
										.createSequentialGroup()
										.addGroup(
												ip_find_item_dialogLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																ip_find_item_dialogLayout
																		.createSequentialGroup()
																		.addGap(
																				140,
																				140,
																				140)
																		.addComponent(
																				ip_fid_itemID_field,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				102,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																ip_find_item_dialogLayout
																		.createSequentialGroup()
																		.addGap(
																				82,
																				82,
																				82)
																		.addComponent(
																				ip_fid_error_label,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				213,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																ip_find_item_dialogLayout
																		.createSequentialGroup()
																		.addGap(
																				176,
																				176,
																				176)
																		.addComponent(
																				ip_fid_itemID_label))
														.addGroup(
																ip_find_item_dialogLayout
																		.createSequentialGroup()
																		.addGap(
																				136,
																				136,
																				136)
																		.addComponent(
																				ip_fid_find_button,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				106,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(105, Short.MAX_VALUE)));
		ip_find_item_dialogLayout.setVerticalGroup(ip_find_item_dialogLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						ip_find_item_dialogLayout.createSequentialGroup()
								.addGap(53, 53, 53).addComponent(
										ip_fid_itemID_label).addGap(18, 18, 18)
								.addComponent(ip_fid_itemID_field,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										34,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(15, 15, 15).addComponent(
										ip_fid_error_label,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										43,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(26, 26, 26).addComponent(
										ip_fid_find_button,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										41,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(54, Short.MAX_VALUE)));

		addToPST_dialog.setMinimumSize(new java.awt.Dimension(900, 600));

		atpst_item_table
				.setModel(new javax.swing.table.DefaultTableModel(
						new Object[][] { { null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null },
								{ null, null, null, null, null } },
						new String[] { "PS Template Name",
								"Multiplier of Price 1", "New Price 1",
								"Multiplier of Price 2", "New Price 2" }) {
					Class[] types = new Class[] { java.lang.String.class,
							java.lang.Float.class, java.lang.Float.class,
							java.lang.Float.class, java.lang.Float.class };
					boolean[] canEdit = new boolean[] { false, true, false,
							true, false };

					public Class getColumnClass(int columnIndex) {
						return types[columnIndex];
					}

					public boolean isCellEditable(int rowIndex, int columnIndex) {
						return canEdit[columnIndex];
					}
				});
		atpst_item_table.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyTyped(java.awt.event.KeyEvent evt) {
				atpst_item_tableKeyTyped(evt);
			}
		});
		jScrollPane1.setViewportView(atpst_item_table);

		atpst_save_button.setText("Save & Go to next item");
		atpst_save_button.setMaximumSize(new java.awt.Dimension(160, 50));
		atpst_save_button.setMinimumSize(new java.awt.Dimension(160, 50));
		atpst_save_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						atpst_save_buttonActionPerformed(evt);
					}
				});

		atpst_id_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_id_label.setText("Item ID");

		atpst_id_field
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_id_field.setText("Item ID");

		atpst_name_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_name_label.setText("Item Name");

		atpst_name_field
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_name_field.setText("Name");

		atpst_unit1_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_unit1_label.setText("Unit 1");

		atpst_unit1_field
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_unit1_field.setText("Unit");

		atpst_unit2_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_unit2_label.setText("Unit 2");

		atpst_unit2_field
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_unit2_field.setText("Unit");

		atpst_price1_field
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_price1_field.setText("Price");

		atpst_price1_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_price1_label.setText("Default Price 1");

		atpst_price2_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_price2_label.setText("Default Price 2");

		atpst_price2_field
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		atpst_price2_field.setText("Price");

		atpst_warning_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

		atpst_counter_field.setText("Item #i of n");

		javax.swing.GroupLayout addToPST_dialogLayout = new javax.swing.GroupLayout(
				addToPST_dialog.getContentPane());
		addToPST_dialog.getContentPane().setLayout(addToPST_dialogLayout);
		addToPST_dialogLayout
				.setHorizontalGroup(addToPST_dialogLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								addToPST_dialogLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												addToPST_dialogLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																addToPST_dialogLayout
																		.createSequentialGroup()
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				876,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																addToPST_dialogLayout
																		.createSequentialGroup()
																		.addGroup(
																				addToPST_dialogLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addGroup(
																								addToPST_dialogLayout
																										.createSequentialGroup()
																										.addComponent(
																												atpst_id_field)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												atpst_name_field,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												175,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								addToPST_dialogLayout
																										.createSequentialGroup()
																										.addComponent(
																												atpst_id_label)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												atpst_name_label,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE)))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addGroup(
																				addToPST_dialogLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								atpst_unit1_field,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								atpst_unit1_label,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								105,
																								Short.MAX_VALUE))
																		.addGap(
																				18,
																				18,
																				18)
																		.addGroup(
																				addToPST_dialogLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								atpst_price1_field,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								atpst_price1_label,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								137,
																								Short.MAX_VALUE))
																		.addGap(
																				47,
																				47,
																				47)
																		.addGroup(
																				addToPST_dialogLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								atpst_unit2_label,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								137,
																								Short.MAX_VALUE)
																						.addComponent(
																								atpst_unit2_field,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								137,
																								Short.MAX_VALUE))
																		.addGap(
																				36,
																				36,
																				36)
																		.addGroup(
																				addToPST_dialogLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								atpst_price2_label,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								142,
																								Short.MAX_VALUE)
																						.addComponent(
																								atpst_price2_field,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								142,
																								Short.MAX_VALUE))
																		.addGap(
																				34,
																				34,
																				34))))
						.addGroup(
								addToPST_dialogLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(atpst_counter_field)
										.addGap(277, 277, 277)
										.addComponent(
												atpst_save_button,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												219,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(331, Short.MAX_VALUE))
						.addGroup(
								addToPST_dialogLayout
										.createSequentialGroup()
										.addGap(230, 230, 230)
										.addComponent(
												atpst_warning_label,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												472,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(198, Short.MAX_VALUE)));
		addToPST_dialogLayout
				.setVerticalGroup(addToPST_dialogLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								addToPST_dialogLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												addToPST_dialogLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																atpst_id_label)
														.addComponent(
																atpst_name_label)
														.addComponent(
																atpst_unit1_label)
														.addComponent(
																atpst_price1_label)
														.addComponent(
																atpst_unit2_label)
														.addComponent(
																atpst_price2_label))
										.addGap(4, 4, 4)
										.addGroup(
												addToPST_dialogLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																atpst_id_field,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																27,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																atpst_name_field)
														.addComponent(
																atpst_unit1_field)
														.addComponent(
																atpst_price1_field)
														.addComponent(
																atpst_unit2_field)
														.addComponent(
																atpst_price2_field))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												375,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												addToPST_dialogLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																addToPST_dialogLayout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				atpst_warning_label,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				34,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				atpst_save_button,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				52,
																				Short.MAX_VALUE))
														.addComponent(
																atpst_counter_field))
										.addGap(52, 52, 52)));

		pd_printText_textarea.setColumns(20);
		pd_printText_textarea.setRows(5);
		pd_printText_scrollpane.setViewportView(pd_printText_textarea);

		javax.swing.GroupLayout print_dialogLayout = new javax.swing.GroupLayout(
				print_dialog.getContentPane());
		print_dialog.getContentPane().setLayout(print_dialogLayout);
		print_dialogLayout
				.setHorizontalGroup(print_dialogLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								print_dialogLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												pd_printText_scrollpane,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												696, Short.MAX_VALUE)
										.addContainerGap()));
		print_dialogLayout
				.setVerticalGroup(print_dialogLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								print_dialogLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												pd_printText_scrollpane,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												374, Short.MAX_VALUE)
										.addContainerGap()));

		setPreferredSize(new java.awt.Dimension(1024, 679));

		ip_itemList_table.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {
						{ null, null, null, null, null, null, null,
								new Boolean(false) },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null },
						{ null, null, null, null, null, null, null, null } },
				new String[] { "ID", "Name", "Description", "1st Unit",
						"Price 1", "2nd Unit", "Price 2",
						"Item Status (active/inactive)" }) {
			Class[] types = new Class[] { java.lang.Integer.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class, java.lang.Float.class,
					java.lang.String.class, java.lang.Float.class,
					java.lang.Boolean.class };
			boolean[] canEdit = new boolean[] { false, false, false, false,
					true, false, true, false };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		ip_itemList_table.setToolTipText("");
		jScrollPane7.setViewportView(ip_itemList_table);

		ip_itemList_label
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		ip_itemList_label.setText("Item List");

		ip_saveChanges_button.setMnemonic(KeyEvent.VK_S);
		ip_saveChanges_button.setText("Save Changes");
		ip_saveChanges_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ip_saveChanges_buttonActionPerformed(evt);
					}
				});

		ip_createNewItem_button.setText("Create New Item");
		ip_createNewItem_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ip_createNewItem_buttonActionPerformed(evt);
					}
				});

		ip_description_label.setText("Description");

		ip_unit1Price_label.setText("Unit 1 Price");

		ip_unit1_label.setText("Unit 1");

		ip_unit2_label.setText("Unit 2");
		ip_unit2_label.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

		ip_itemStatus_label.setText("Item Status");

		ip_itemStatus_field.setText("Active?");

		ip_itemName_label.setText("Item Name");

		ip_itemID_label.setText("Item ID");

		ip_itemID_field.setEditable(false);

		ip_findItem_button.setText("Select Item");
		ip_findItem_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ip_findItem_buttonActionPerformed(evt);
					}
				});

		ip_unit2Price_label.setText("Unit 2 Price");

		ip_confirm_button.setText("Confirm Changes");
		ip_confirm_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ip_confirmChanges_buttonActionPerformed(evt);
					}
				});

		ip_progressBar_label.setText("SAVING...");

		ip_reloadTable_button.setText("Reload Table");
		ip_reloadTable_button
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ip_reloadTable_buttonActionPerformed(evt);
					}
				});

		ip_print_button.setText("Print");
		ip_print_button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ip_print_buttonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																ip_itemList_label)
														.addComponent(
																jScrollPane7,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																956,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addComponent(
																												ip_errorLabel,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												673,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																												51,
																												Short.MAX_VALUE)
																										.addComponent(
																												ip_print_button)
																										.addGap(
																												42,
																												42,
																												42)
																										.addComponent(
																												ip_reloadTable_button,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												115,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addGroup(
																																layout
																																		.createSequentialGroup()
																																		.addGroup(
																																				layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING,
																																								false)
																																						.addComponent(
																																								ip_itemID_field)
																																						.addComponent(
																																								ip_itemID_label,
																																								javax.swing.GroupLayout.DEFAULT_SIZE,
																																								javax.swing.GroupLayout.DEFAULT_SIZE,
																																								Short.MAX_VALUE))
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addGroup(
																																				layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING)
																																						.addComponent(
																																								ip_itemName_field,
																																								javax.swing.GroupLayout.PREFERRED_SIZE,
																																								171,
																																								javax.swing.GroupLayout.PREFERRED_SIZE)
																																						.addComponent(
																																								ip_itemName_label))
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addGroup(
																																				layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING)
																																						.addComponent(
																																								ip_description_label,
																																								javax.swing.GroupLayout.DEFAULT_SIZE,
																																								385,
																																								Short.MAX_VALUE)
																																						.addComponent(
																																								ip_description_field,
																																								javax.swing.GroupLayout.PREFERRED_SIZE,
																																								385,
																																								javax.swing.GroupLayout.PREFERRED_SIZE)))
																														.addGroup(
																																layout
																																		.createSequentialGroup()
																																		.addComponent(
																																				ip_findItem_button,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				113,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addGap(
																																				43,
																																				43,
																																				43)
																																		.addComponent(
																																				ip_createNewItem_button,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				166,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addGroup(
																																				layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING)
																																						.addGroup(
																																								layout
																																										.createSequentialGroup()
																																										.addGap(
																																												18,
																																												18,
																																												18)
																																										.addComponent(
																																												ip_progressBar,
																																												javax.swing.GroupLayout.PREFERRED_SIZE,
																																												169,
																																												javax.swing.GroupLayout.PREFERRED_SIZE))
																																						.addGroup(
																																								layout
																																										.createSequentialGroup()
																																										.addGap(
																																												73,
																																												73,
																																												73)
																																										.addComponent(
																																												ip_progressBar_label,
																																												javax.swing.GroupLayout.PREFERRED_SIZE,
																																												65,
																																												javax.swing.GroupLayout.PREFERRED_SIZE)))))
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING)
																														.addGroup(
																																javax.swing.GroupLayout.Alignment.LEADING,
																																layout
																																		.createSequentialGroup()
																																		.addGroup(
																																				layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING)
																																						.addComponent(
																																								ip_unit1_label)
																																						.addComponent(
																																								ip_unit1_field,
																																								javax.swing.GroupLayout.PREFERRED_SIZE,
																																								40,
																																								javax.swing.GroupLayout.PREFERRED_SIZE))
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addGroup(
																																				layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING)
																																						.addGroup(
																																								layout
																																										.createSequentialGroup()
																																										.addComponent(
																																												ip_unit1Price_field,
																																												javax.swing.GroupLayout.PREFERRED_SIZE,
																																												66,
																																												javax.swing.GroupLayout.PREFERRED_SIZE)
																																										.addPreferredGap(
																																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																										.addComponent(
																																												ip_unit2_field,
																																												javax.swing.GroupLayout.PREFERRED_SIZE,
																																												65,
																																												javax.swing.GroupLayout.PREFERRED_SIZE))
																																						.addGroup(
																																								layout
																																										.createSequentialGroup()
																																										.addComponent(
																																												ip_unit1Price_label)
																																										.addPreferredGap(
																																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																										.addComponent(
																																												ip_unit2_label)))
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addGroup(
																																				layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING,
																																								false)
																																						.addGroup(
																																								layout
																																										.createSequentialGroup()
																																										.addComponent(
																																												ip_unit2Price_label)
																																										.addGap(
																																												12,
																																												12,
																																												12)
																																										.addComponent(
																																												ip_itemStatus_label,
																																												javax.swing.GroupLayout.DEFAULT_SIZE,
																																												javax.swing.GroupLayout.DEFAULT_SIZE,
																																												Short.MAX_VALUE))
																																						.addGroup(
																																								layout
																																										.createSequentialGroup()
																																										.addComponent(
																																												ip_unit2Price_field,
																																												javax.swing.GroupLayout.PREFERRED_SIZE,
																																												53,
																																												javax.swing.GroupLayout.PREFERRED_SIZE)
																																										.addPreferredGap(
																																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																										.addComponent(
																																												ip_itemStatus_field))))
																														.addGroup(
																																layout
																																		.createSequentialGroup()
																																		.addComponent(
																																				ip_confirm_button,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				144,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addComponent(
																																				ip_saveChanges_button)))))
																		.addGap(
																				85,
																				85,
																				85)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(ip_itemList_label)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jScrollPane7,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												437,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				29,
																				29,
																				29)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								ip_itemID_label)
																						.addComponent(
																								ip_itemName_label))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								ip_itemID_field,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								21,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								ip_itemName_field,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								ip_description_field,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				28,
																				28,
																				28)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.BASELINE)
																										.addComponent(
																												ip_unit1_label)
																										.addComponent(
																												ip_unit1Price_label)
																										.addComponent(
																												ip_unit2_label))
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addGap(
																												1,
																												1,
																												1)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.BASELINE)
																														.addComponent(
																																ip_unit2Price_label)
																														.addComponent(
																																ip_itemStatus_label))
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.BASELINE)
																														.addComponent(
																																ip_unit1_field,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE)
																														.addComponent(
																																ip_unit1Price_field,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE)
																														.addComponent(
																																ip_unit2_field,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE)
																														.addComponent(
																																ip_itemStatus_field)
																														.addComponent(
																																ip_unit2Price_field,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE)))
																						.addComponent(
																								ip_description_label))))
										.addGap(7, 7, 7)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				7,
																				7,
																				7)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																						.addGroup(
																								layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.BASELINE)
																										.addComponent(
																												ip_findItem_button,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												61,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addComponent(
																												ip_createNewItem_button,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												56,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.BASELINE)
																										.addComponent(
																												ip_saveChanges_button,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												54,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addComponent(
																												ip_confirm_button,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												61,
																												Short.MAX_VALUE))))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				ip_progressBar_label)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				ip_progressBar,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(
																				7,
																				7,
																				7)))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																layout
																		.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.BASELINE)
																		.addComponent(
																				ip_reloadTable_button)
																		.addComponent(
																				ip_print_button))
														.addComponent(
																ip_errorLabel,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																31,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));

		layout.linkSize(javax.swing.SwingConstants.VERTICAL,
				new java.awt.Component[] { ip_confirm_button,
						ip_createNewItem_button, ip_findItem_button,
						ip_saveChanges_button });

	}// </editor-fold>
	//GEN-END:initComponents

	private void ip_print_buttonActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		this.printAllItems();
	}

	private void atpst_item_tableKeyTyped(java.awt.event.KeyEvent evt) {
		atpst_warning_label.setText("");
		atpst_save_button.setEnabled(true);
		// Run through the table and update all price values
		int row = 0;
		while (atpst_item_table.getValueAt(row, ATPST_PSTNAME_COLUMN) != null) {
			// unit 1 price with new modifier
			float m1 = (Float) atpst_item_table.getValueAt(row,
					ATPST_UNIT1PRICE_COLUMN);
			float p1 = m1 * Float.parseFloat(this.atpst_price1_field.getText());

			// unit 2 price with new modifier
			float m2 = (Float) atpst_item_table.getValueAt(row,
					ATPST_UNIT2PRICE_COLUMN);
			float p2 = m2 * Float.parseFloat(this.atpst_price2_field.getText());

			// If we have positive nonzero values we write the new prices to the table
			if (m1 > 0 && m2 > 0) {
				atpst_item_table.setValueAt(p1, row, ATPST_NEWPRICE1_COLUMN);
				atpst_item_table.setValueAt(p2, row, ATPST_NEWPRICE2_COLUMN);
			} else { // otherwise we put up the warning and don't do anything else
				String warningText = WARNING_START;
				warningText += "Only positive, nonzero values are allowed.";
				warningText += WARNING_END;
				atpst_warning_label.setText(warningText);
				atpst_save_button.setEnabled(false);
			}
			row++;
		}
	}

	////////////////////////////////////////////////////////////////
	// We work through all items we need to save to DB one by one //
	////////////////////////////////////////////////////////////////
	private void atpst_save_buttonActionPerformed(java.awt.event.ActionEvent evt) {
		int max = currentlyInactiveItems.size() + currentNewItems.size();

		if (this.allValuesArePositive()) {
			// Save the current data to all price sheets & increment counter
			this.saveToPSTs();
			addItemCounter++;

			// Now that we're done with that, we set up the next item in the list
			this.setUpNextItem();

			// If addItemCounter > max, we're done saving to PSTs
			// So we close the window and notify the user that we're done
			if (addItemCounter > max) {
				addToPST_dialog.setVisible(false);
				notificationDialog.displayNotification("<html>All items have been saved to price sheet templates!");
				// Clear the arraylists since we're done with them & reset counter
				currentlyInactiveItems.clear();
				currentNewItems.clear();
				addItemCounter = 1;
			}
		} else {
			String warningText = WARNING_START;
			warningText += "Only positive, nonzero values are allowed.";
			warningText += WARNING_END;
			atpst_warning_label.setText(warningText);
		}

		// Need to remember to .clear() currentlyInactiveItems/currentNewItems once finished
		// Also remember to reset the addItemCounter to 1 once done
	}

	// Returns true if all values in atpst_item_table are positive
	// Returns false otherwise
	private boolean allValuesArePositive() {
		// Go through the table, and if any unit prices are <= 0, return false
		int row = 0;
		while (atpst_item_table.getValueAt(row, ATPST_PSTNAME_COLUMN) != null) {
			float p1 = (Float) atpst_item_table.getValueAt(row,
					ATPST_UNIT1PRICE_COLUMN);
			float p2 = (Float) atpst_item_table.getValueAt(row,
					ATPST_UNIT2PRICE_COLUMN);
			if (p1 <= 0 || p2 <= 0) {
				return false;
			}

			row++;
		}

		// We got through without tripping anything, so everything is positive
		return true;
	}

	// Grabs whatever item is in the current addToPST_dialog and
	// then saves it to all PSTs listed
	private void saveToPSTs() {

		boolean isNew;
		// Means that we're still working through the old items, so isNew is false
		if (addItemCounter <= currentlyInactiveItems.size()) {
			isNew = false;
		} else { // Means we're done with the old items, so the current item is new
			isNew = true;
		}

		ArrayList<Pricesheetitems> modQueue = new ArrayList<Pricesheetitems>();

		// Create Pricesheetitems for all values on the table then add it to DB
		int row = 0;
		while (atpst_item_table.getValueAt(row, ATPST_PSTNAME_COLUMN) != null) {
			Pricesheetitems psItem = new Pricesheetitems();
			int itemID = Integer.parseInt(atpst_id_field.getText());
			psItem.setItemID(itemID);
			int psID = MEDIATOR.getPSid((String) atpst_item_table.getValueAt(
					row, ATPST_PSTNAME_COLUMN));
			if (!isNew) { // only set price sheet item ID if it's an old item
				psItem.setPriceSheetItemID(MEDIATOR.getPSIid(psID, itemID));
			}
			psItem.setPriceSheetID(psID);
			psItem.setPrice((Float) atpst_item_table.getValueAt(row,
					ATPST_UNIT1PRICE_COLUMN));
			psItem.setOtherUnitPrice((Float) atpst_item_table.getValueAt(row,
					ATPST_UNIT2PRICE_COLUMN));
			modQueue.add(psItem);
			row++;
		}

		if (isNew) { // if new we add all pricesheetitems to the DB
			MEDIATOR.savePSadditions(modQueue);
		} else { // else we modify all pricesheetitems in DB
			MEDIATOR.savePSmodifications(modQueue);
		}
	}

	// Sets up the next item that we need to integrate/reintegrate into PSTs
	private void setUpNextItem() {
		int max = currentlyInactiveItems.size() + currentNewItems.size();
		// Means that we're still working through items
		if (addItemCounter <= max) {
			// Means we're still working through the old items
			if (addItemCounter <= currentlyInactiveItems.size()) {
				int itemID = currentlyInactiveItems.get(addItemCounter - 1);
				Items curItem = MEDIATOR.getItem(itemID);
				this.fillOutATPSTdialog(curItem);
			}
			// Now we're on the new items
			else {
				int getIndex = (addItemCounter - 1) % currentNewItems.size();
				String itemName = currentNewItems.get(getIndex);
				Items curItem = MEDIATOR.getItemFromList(itemName);
				this.fillOutATPSTdialog(curItem);
			}
		}

		// Else we're done and don't need to do anything
	}

	private void ip_confirmChanges_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {

		System.out.println("Button pressed");
		if (validateFields()) {

			//get itemID
			int itemID = Integer.parseInt(this.ip_itemID_field.getText());

			//check if it's new
			boolean new_item_flag = false;
			if (itemID == -1) {
				new_item_flag = true;
			}

			//try finding the item in table
			boolean end_found_flag = false;
			int i = 0;
			while (!end_found_flag) {
				Object cell_value = this.ip_itemList_table.getValueAt(i,
						IP_ID_COLUMN);
				int thisID = 0;
				if (cell_value != null) {
					thisID = (Integer) cell_value;
				}

				if (!new_item_flag) {
					//if item is found
					if (thisID == itemID) {
						this.modifyInfoInRow(i);
						// If the item isn't active, we add its index to the list
						if (MEDIATOR.isItemInactiveInDB(itemID)) {
							this.currentlyInactiveItems.add(itemID);
						}
						end_found_flag = true;
					}
				}

				//if found the last filled item
				if (thisID == 0) {
					end_found_flag = true;
					if (!new_item_flag) {
						System.out
								.println("item not found... although should. spooky.");
					}

					//adding new item
					else {
						this.modifyInfoInRow(i);
					}
				}

				i++;

			}
		}
	}

	private void ip_reloadTable_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {
		this.fillOutTheTable();
		this.clearInputFields();
	}

	private void ip_createNewItem_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {

		this.enableChangeButtons(true);
		this.ip_itemID_field.setText("-1");
		this.clearInputFields();
		this.ip_itemStatus_field.setSelected(true);
	}

	public void clearInputFields() {
		this.ip_description_field.setText("");
		this.ip_itemName_field.setText("");
		this.ip_errorLabel.setText("");
		this.ip_unit1Price_field.setText("");
		this.ip_unit2Price_field.setText("");
		this.ip_unit1_field.setText("");
		this.ip_unit2_field.setText("");
	}

	private void ip_fid_find_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {
		//pick up itemID from field
		String itmID = this.ip_fid_itemID_field.getText();

		//check if it contains any errors
		try {
			int itemID = Integer.parseInt(itmID);

			Items itm = MEDIATOR.getItem(itemID);
			//check if it exists
			if (itm == null) {
				//display errors
				this.ip_fid_error_label
						.setText("Item with this ID doesn't exist");
			} else {
				boolean isSelected = false;
				if (itm.getIsActive() == 1) {
					isSelected = true;
				}
				//fill in the correct field on item panel
				this.ip_itemID_field.setText(Integer.toString(itm.getItemID()));
				this.ip_description_field.setText(itm.getItemDescription());
				this.ip_itemName_field.setText(itm.getItemName());
				this.ip_unit1_field.setText(itm.getUnitName());
				this.ip_unit1Price_field.setText(Double.toString(itm
						.getUnitPrice()));//TODO:BUG unjit
				this.ip_unit2_field.setText(itm.getOtherUnitName());
				this.ip_unit2Price_field.setText(Double.toString(itm
						.getOtherUnitPrice()));//TODO:BUG unit
				this.ip_itemStatus_field.setSelected(isSelected);

				this.ip_find_item_dialog.setVisible(false);
				this.ip_fid_find_button.setEnabled(false);
			}

		} catch (NumberFormatException e) {
			this.ip_fid_error_label.setText("Please enter a valid number");
			this.ip_fid_itemID_field.setText("");
		}

	}

	private void ip_fid_itemID_fieldActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void ip_findItem_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {

		this.enableChangeButtons(true);
		this.ip_find_item_dialog.setVisible(true);
		this.ip_fid_find_button.setEnabled(true);
		clearInputFields();

	}

	private void ip_saveChanges_buttonActionPerformed(
			java.awt.event.ActionEvent evt) {

		////////////////////
		//copy DB to array//
		////////////////////
		ArrayList<Items> _itmsArr = new ArrayList<Items>();

		int i = 0;

		//go through all the table
		while (this.ip_itemList_table.getValueAt(i, IP_ID_COLUMN) != null) {
			/////////////////////////////////
			//put item info into item object/
			/////////////////////////////////
			Items _itm = new Items();
			_itm.setItemID((Integer) this.ip_itemList_table.getValueAt(i,
					IP_ID_COLUMN));
			_itm.setItemName((String) this.ip_itemList_table.getValueAt(i,
					IP_NAME_COLUMN));
			_itm.setItemDescription((String) this.ip_itemList_table.getValueAt(
					i, IP_DESCRIPTION_COLUMN));
			_itm.setUnitName((String) this.ip_itemList_table.getValueAt(i,
					IP_UNIT1_COLUMN));
			_itm.setUnitPrice((Float) this.ip_itemList_table.getValueAt(i,
					IP_UNIT1PRICE_COLUMN));
			_itm.setOtherUnitName((String) this.ip_itemList_table.getValueAt(i,
					IP_UNIT2_COLUMN));
			_itm.setOtherUnitPrice((Float) this.ip_itemList_table.getValueAt(i,
					IP_UNIT2PRICE_COLUMN));
			if ((Boolean) this.ip_itemList_table
					.getValueAt(i, IP_STATUS_COLUMN)) {
				_itm.setIsActive((short) 1);
			} else {
				_itm.setIsActive((short) 0);
			}
			_itmsArr.add(_itm);

			// If the item ID < 0, we're dealing with a new item
			// So we save its name into the currentNewItems array
			if (_itm.getItemID() < 0) {
				this.currentNewItems.add(_itm.getItemName());
			}

			i++;

		}

		/////////////////
		//pass array to// 
		/////////////////
		this.ip_progressBar_label.setVisible(true);
		this.ip_progressBar.setVisible(true);
		this.ip_progressBar.setMaximum(_itmsArr.size());

		if (MEDIATOR.saveItems(_itmsArr)) {
			this.ip_errorLabel.setText("SAVED");
			this.fillOutTheTable();

			// Now we deal with all the items we wish to integrate/reintegrate
			// into the price sheet templates
			this.fillFirstItem();

		} else {
			this.ip_errorLabel.setText("ERROR! SAVING FAILED!");
			this.ip_progressBar_label.setVisible(false);
			this.ip_progressBar.setVisible(false);
		}
		//reload screen?

	}

	public void set_ip_ProgressBarAt(int value) {
		this.ip_progressBar.setValue(value);
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JDialog addToPST_dialog;
	private javax.swing.JLabel atpst_counter_field;
	private javax.swing.JLabel atpst_id_field;
	private javax.swing.JLabel atpst_id_label;
	private javax.swing.JTable atpst_item_table;
	private javax.swing.JLabel atpst_name_field;
	private javax.swing.JLabel atpst_name_label;
	private javax.swing.JLabel atpst_price1_field;
	private javax.swing.JLabel atpst_price1_label;
	private javax.swing.JLabel atpst_price2_field;
	private javax.swing.JLabel atpst_price2_label;
	private javax.swing.JButton atpst_save_button;
	private javax.swing.JLabel atpst_unit1_field;
	private javax.swing.JLabel atpst_unit1_label;
	private javax.swing.JLabel atpst_unit2_field;
	private javax.swing.JLabel atpst_unit2_label;
	private javax.swing.JLabel atpst_warning_label;
	private javax.swing.JButton ip_confirm_button;
	private javax.swing.JButton ip_createNewItem_button;
	private javax.swing.JTextField ip_description_field;
	private javax.swing.JLabel ip_description_label;
	private javax.swing.JLabel ip_errorLabel;
	private javax.swing.JLabel ip_fid_error_label;
	private javax.swing.JButton ip_fid_find_button;
	private javax.swing.JTextField ip_fid_itemID_field;
	private javax.swing.JLabel ip_fid_itemID_label;
	private javax.swing.JButton ip_findItem_button;
	private javax.swing.JDialog ip_find_item_dialog;
	private javax.swing.JTextField ip_itemID_field;
	private javax.swing.JLabel ip_itemID_label;
	private javax.swing.JLabel ip_itemList_label;
	private javax.swing.JTable ip_itemList_table;
	private javax.swing.JTextField ip_itemName_field;
	private javax.swing.JLabel ip_itemName_label;
	private javax.swing.JCheckBox ip_itemStatus_field;
	private javax.swing.JLabel ip_itemStatus_label;
	private javax.swing.JButton ip_print_button;
	private javax.swing.JProgressBar ip_progressBar;
	private javax.swing.JLabel ip_progressBar_label;
	private javax.swing.JButton ip_reloadTable_button;
	private javax.swing.JButton ip_saveChanges_button;
	private javax.swing.JTextField ip_unit1Price_field;
	private javax.swing.JLabel ip_unit1Price_label;
	private javax.swing.JTextField ip_unit1_field;
	private javax.swing.JLabel ip_unit1_label;
	private javax.swing.JTextField ip_unit2Price_field;
	private javax.swing.JLabel ip_unit2Price_label;
	private javax.swing.JTextField ip_unit2_field;
	private javax.swing.JLabel ip_unit2_label;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane7;
	private javax.swing.JScrollPane pd_printText_scrollpane;
	private javax.swing.JTextArea pd_printText_textarea;
	private javax.swing.JDialog print_dialog;
	// End of variables declaration//GEN-END:variables
	private com.popsGUI.java.notificationDialog notificationDialog;
	//TODO: the start of our code
	////////////////Our variable declarations /////////////////
	//	CustomersDao CustomersDAO = CustomersDaoFactory.create();
	//	ItemsDao ItemsDAO = ItemsDaoFactory.create();
	//	OrdereditemsDao OrdereditemsDAO = OrdereditemsDaoFactory.create();
	//	OrdersDao OrdersDAO = OrdersDaoFactory.create();

	Mediator MEDIATOR = Mediator.getInstance();

	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
	DecimalFormat decimalPriceFormat = new DecimalFormat("###,###,##0.00");

	final int IP_ID_COLUMN = 0;
	final int IP_NAME_COLUMN = 1;
	final int IP_DESCRIPTION_COLUMN = 2;
	final int IP_UNIT1_COLUMN = 3;
	final int IP_UNIT1PRICE_COLUMN = 4;
	final int IP_UNIT2_COLUMN = 5;
	final int IP_UNIT2PRICE_COLUMN = 6;
	final int IP_STATUS_COLUMN = 7;

	final int ATPST_PSTNAME_COLUMN = 0;
	final int ATPST_UNIT1PRICE_COLUMN = 1;
	final int ATPST_NEWPRICE1_COLUMN = 2;
	final int ATPST_UNIT2PRICE_COLUMN = 3;
	final int ATPST_NEWPRICE2_COLUMN = 4;

	final int PRICE1_INDEX = 0;
	final int PRICE2_INDEX = 1;

	final int FIRST_ITEM_INDEX = 0;

	private int addItemCounter = 1;

	final String WARNING_START = "<html><font color=red>";
	final String WARNING_END = "</font></html>";

	final String PRINT_HEADER = "ID \tName \t\t\t\tUnit 1 \tPrice 1 \tUnit 2 \tPrice 2 \tActive \n";
	final int MAX_TABS = 4;
	final int TAB_LENGTH = 13;

	// Used to save the item IDs of the items changing from inactive to active
	// Helps with integration into pricesheet templates
	ArrayList<Integer> currentlyInactiveItems = new ArrayList<Integer>();

	// Used to save the indexes of the new items
	// Helps with integration into pricesheet templates
	ArrayList<String> currentNewItems = new ArrayList<String>();

	///////////// End of our variable declarations /////////////

	// Disables or enables all interactive components in the item panel
	public void setItemPanelComponentsEnabledPrameterTo(boolean flag) {
		this.setEnabled(flag);
		this.ip_findItem_button.setEnabled(flag);
		this.ip_saveChanges_button.setEnabled(flag);
		this.ip_createNewItem_button.setEnabled(flag);
		this.ip_print_button.setEnabled(flag);

	}

	public void modifyInfoInRow(int rowNum) {

		this.ip_errorLabel.setText("Found a sopt at:"
				+ Integer.toString(rowNum));

		this.ip_itemList_table.setValueAt(Integer.parseInt(this.ip_itemID_field
				.getText()), rowNum, IP_ID_COLUMN);
		this.ip_itemList_table.setValueAt(this.ip_itemName_field.getText(),
				rowNum, IP_NAME_COLUMN);
		this.ip_itemList_table.setValueAt(this.ip_description_field.getText(),
				rowNum, IP_DESCRIPTION_COLUMN);
		this.ip_itemList_table.setValueAt(this.ip_unit1_field.getText(),
				rowNum, IP_UNIT1_COLUMN);
		this.ip_itemList_table.setValueAt(Float
				.parseFloat(this.ip_unit1Price_field.getText()), rowNum,
				IP_UNIT1PRICE_COLUMN);//TODO: BUG UNIT
		this.ip_itemList_table.setValueAt(this.ip_unit2_field.getText(),
				rowNum, IP_UNIT2_COLUMN);
		this.ip_itemList_table.setValueAt(Float
				.parseFloat(this.ip_unit2Price_field.getText()), rowNum,
				IP_UNIT2PRICE_COLUMN);
		this.ip_itemList_table
				.setValueAt(this.ip_itemStatus_field.isSelected(), rowNum,
						IP_STATUS_COLUMN);

	}

	public boolean validateFields() {
		String error = "";

		//validate name
		String itemName = this.ip_itemName_field.getText();
		//no name entered
		if (this.ip_itemID_field.getText().equals("")) {
			error += "  | please Create or Select item |  ";
		}

		if (itemName.equals("")) {
			error += "  | name |  ";
		} else {
			//validate if name already exists in db
			if (this.ip_itemID_field.getText().equals("-1")) {
				if (!MEDIATOR.validateItemName(itemName)) {
					error += "  | name already exists |  ";
					System.out.println("Name Bug found");
				}
			}
			//validate 	if name already exists in list
			boolean flag = true;
			int row = 0;
			if (this.ip_itemID_field.getText().length() > MEDIATOR.I_MAX_NAME_LENGTH) {
				error += "  | name is too long |  ";
			}

			while (flag) {
				Object obj = this.ip_itemList_table.getValueAt(row,
						IP_ID_COLUMN);
				if (obj == null) {
					flag = false;
				} else {
					if ((Integer) obj == -1) {
						if (itemName.equals((String) this.ip_itemList_table
								.getValueAt(row, IP_NAME_COLUMN))) {
							error += "  | name already exists |  ";
							System.out.println("Name Bug found");
						}
					}
				}

				row++;
			}
		}

		if (this.ip_itemID_field.getText().length() > MEDIATOR.I_MAX_NAME_LENGTH) {
			error += "  | name is too long |  ";
		}

		//TODO: ENABLE
		//		//validate description
		//		if (this.ip_description_field.getText().equals("")) {
		//			error += "  description  ";
		//		}

		//validate unit1name
		if (this.ip_unit1_field.getText().equals("")) {
			error += "  | unit1 name |  ";
		}
		if (this.ip_unit1_field.getText().length() > MEDIATOR.I_MAX_UNIT_LENGTH) {
			error += "  | unit1 name too long |  ";
		}

		//validate unit2name
		if (this.ip_unit2_field.getText().equals("")) {
			error += "  | unit2 name |  ";
		}
		if (this.ip_unit2_field.getText().length() > MEDIATOR.I_MAX_UNIT_LENGTH) {
			error += "  | unit2 name too long |  ";
		}

		//validate price 1
		try {
			double unit1p = Double.parseDouble(this.ip_unit1Price_field
					.getText());
			if (unit1p < 0) {
				error += "  | unit1 price too low |  ";
			}
		} catch (NumberFormatException e) {
			error += "  | unit1 price |  ";
		}

		//validate price 2
		try {
			double unit2p = Double.parseDouble(this.ip_unit2Price_field
					.getText());
			if (unit2p < 0) {
				error += "  | unit2 price too low |  ";
			}
		} catch (NumberFormatException e) {
			error += "  | unit2 price |  ";
		}

		if (error.equals("")) {
			return true;
		} else {
			error = "ERROR! Please correct the following fields:" + error;
			this.displayNotification(error);
			this.ip_errorLabel.setText(error);

			return false;
		}

	}

	public void fillOutTheTable() {
		ArrayList<Items> items_list = MEDIATOR.getItemsList();

		this.ip_find_item_dialog.setVisible(false);
		this.ip_fid_find_button.setEnabled(false);
		this.ip_progressBar_label.setVisible(false);
		this.ip_progressBar.setVisible(false);

		// fill the table with info from the items_list
		boolean status;

		for (int i = 0; i < items_list.size(); i++) {
			if (items_list.get(i).getIsActive() == 1) {
				status = true;
			} else {
				status = false;
			}

			this.ip_itemList_table.setValueAt(items_list.get(i).getItemID(), i,
					0);
			this.ip_itemList_table.setValueAt(items_list.get(i).getItemName(),
					i, 1);
			this.ip_itemList_table.setValueAt(items_list.get(i)
					.getItemDescription(), i, 2);
			this.ip_itemList_table.setValueAt(items_list.get(i).getUnitName(),
					i, 3);
			this.ip_itemList_table.setValueAt(items_list.get(i).getUnitPrice(),
					i, 4);
			this.ip_itemList_table.setValueAt(items_list.get(i)
					.getOtherUnitName(), i, 5);
			this.ip_itemList_table.setValueAt(items_list.get(i)
					.getOtherUnitPrice(), i, 6);
			this.ip_itemList_table.setValueAt(status, i, 7);

		}
	}

	// Helper method for adding/reintegrating items into all price sheet templates
	private void fillFirstItem() {
		int max = currentlyInactiveItems.size() + currentNewItems.size();
		// If max >0, we have items to add, so we pick the appropriate item
		if (max > 0) {
			Items curItem;
			this.addToPST_dialog.setVisible(true);
			if (!currentlyInactiveItems.isEmpty()) {
				curItem = MEDIATOR.getItem(currentlyInactiveItems
						.get(FIRST_ITEM_INDEX));
			} else { // there are no inactive items to reactivate
				// but we do have 1+ items to add, so it must be a new item
				curItem = MEDIATOR.getItemFromList(currentNewItems
						.get(FIRST_ITEM_INDEX));
			}
			this.fillOutATPSTdialog(curItem);
		}
	}

	// Fills out item information (ID, Name, units, prices) in addToPST_dialog
	private void fillOutATPSTdialog(Items item) {
		this.setCounterText();

		// Fills out item information (ID, name, units, prices) in addToPST_dialog
		this.atpst_id_field.setText(String.valueOf(item.getItemID()));
		this.atpst_name_field.setText(item.getItemName());
		this.atpst_unit1_field.setText(item.getUnitName());
		this.atpst_price1_field.setText(String.valueOf(item.getUnitPrice()));
		this.atpst_unit2_field.setText(item.getOtherUnitName());
		this.atpst_price2_field.setText(String
				.valueOf(item.getOtherUnitPrice()));

		ArrayList<Pricesheets> psList = MEDIATOR.getPriceSheetTemplates();
		// Run through all templates
		for (int row = 0; row < psList.size(); row++) {
			// price sheet template name
			this.atpst_item_table.setValueAt(psList.get(row)
					.getPriceSheetName(), row, ATPST_PSTNAME_COLUMN);

			float[] itemPrices = MEDIATOR.getItemPrices(psList.get(row)
					.getPriceSheetID(), item.getItemID());

			// Unit 1 modifier
			float m1 = itemPrices[PRICE1_INDEX];
			atpst_item_table.setValueAt(m1, row, ATPST_UNIT1PRICE_COLUMN);

			// Unit 1 price
			float p1 = m1 * item.getUnitPrice();
			atpst_item_table.setValueAt(p1, row, ATPST_NEWPRICE1_COLUMN);

			// Unit 2 modifier
			float m2 = itemPrices[PRICE2_INDEX];
			atpst_item_table.setValueAt(m2, row, ATPST_UNIT2PRICE_COLUMN);

			// Unit 2 price
			float p2 = m2 * item.getOtherUnitPrice();
			atpst_item_table.setValueAt(p2, row, ATPST_NEWPRICE2_COLUMN);
		}

	}

	// Sets the counter text at the bottom left of the addToPST_dialog
	// Also changes the save button's text based on # of items left
	private void setCounterText() {
		int max = currentlyInactiveItems.size() + currentNewItems.size();
		String cText = "";
		cText += "Item " + addItemCounter + " of " + max;
		atpst_counter_field.setText(cText);

		// If we only have one item left, then
		// 'save & go to next item' would be a little weird
		// So the button text is changed
		if (addItemCounter >= max) {
			this.atpst_save_button.setText("Save & Close Window");
		} else {
			this.atpst_save_button.setText("Save & Go to next item");
		}
	}

	//TODO: fix
	public void enableChangeButtons(boolean enable) {
		this.ip_confirm_button.setEnabled(enable);
		//		jButton1.setEnabled(enable);
		ip_saveChanges_button.setEnabled(enable);
	}

	public void displayNotification(String str) {
		//format string
		String finalStr = "";
		System.out.println("str.lenght: " + str.length() + " ::" + str);
		if (str.length() < 50) {
			System.out.println("passed");
			finalStr = str;
		} else {
			System.out.println("formatting:");
			finalStr = finalStr.concat("<html><p>");
			int processed = 0;
			boolean flag = true;

			while (flag) {
				int end_index = processed + 50;
				if (end_index > str.length()) {
					flag = false;
					end_index = str.length();
				}

				finalStr = finalStr.concat(str.substring(processed, end_index));

				finalStr = finalStr.concat("<p>");
				processed = end_index;
				System.out.println(processed + " : " + end_index + "::"
						+ finalStr);
			}

			finalStr = finalStr.concat("</html></p>");
			System.out.println(finalStr);

		}

		//display string
		notificationDialog.displayNotification(finalStr);
	}

	/////////////////////////////////////////////////////
	///////////// Start of printing methods /////////////
	/////////////////////////////////////////////////////

	public void printAllItems() {
		//TODO
		// \t = tab
		// \n = new line
		String itemList = PRINT_HEADER;

		int row = 0;
		while (ip_itemList_table.getValueAt(row, this.IP_ID_COLUMN) != null) {
			// ID
			int ID = (Integer) ip_itemList_table.getValueAt(row,
					this.IP_ID_COLUMN);
			itemList += ID + "\t";

			// Name
			String name = (String) ip_itemList_table.getValueAt(row,
					this.IP_NAME_COLUMN);
			itemList += name;
			int tabs = MAX_TABS - (name.length() / TAB_LENGTH);
			for (int i = 0; i < tabs; i++) {
				itemList += "\t";
			}

			// Unit 1
			String unit1 = (String) ip_itemList_table.getValueAt(row,
					this.IP_UNIT1_COLUMN);
			itemList += unit1 + "\t";

			// Price 1
			Float price1 = (Float) ip_itemList_table.getValueAt(row,
					this.IP_UNIT1PRICE_COLUMN);
			itemList += this.decimalPriceFormat.format(price1.doubleValue())
					+ "\t";

			// Unit 2
			String unit2 = (String) ip_itemList_table.getValueAt(row,
					this.IP_UNIT2_COLUMN);
			itemList += unit2 + "\t";

			// Price 2
			Float price2 = (Float) ip_itemList_table.getValueAt(row,
					this.IP_UNIT2PRICE_COLUMN);
			itemList += this.decimalPriceFormat.format(price2.doubleValue())
					+ "\t";

			// Status
			boolean isActive = (Boolean) ip_itemList_table.getValueAt(row,
					this.IP_STATUS_COLUMN);
			if (isActive) {
				itemList += "Yes\n";
			} else {
				itemList += "No\n";
			}

			row++;
		}

		this.pd_printText_textarea.setText(itemList);
		componentToBePrinted = this.pd_printText_textarea;

		// If I don't set the dialog visible it won't print
		// For some reason, this works...
		this.print_dialog.setVisible(true);
		this.print_dialog.setVisible(false);

		this.print();
	}

	private Component componentToBePrinted;

	public void print() {
		PrinterJob printJob = PrinterJob.getPrinterJob();
		printJob.setPrintable(this);
		if (printJob.printDialog())
			try {
				System.out.println("Calling PrintJob.print()");
				printJob.print();
				System.out.println("End PrintJob.print()");
			} catch (PrinterException pe) {
				System.out.println("Error printing: " + pe);
			}
	}

	public int print(Graphics g, PageFormat pf, int pageIndex) {
		int response = NO_SUCH_PAGE;

		Graphics2D g2 = (Graphics2D) g;

		//  for faster printing, turn off double buffering
		disableDoubleBuffering(componentToBePrinted);

		Dimension d = componentToBePrinted.getSize(); //get size of document
		double panelWidth = d.width; //width in pixels
		double panelHeight = d.height; //height in pixels

		double pageHeight = pf.getImageableHeight(); //height of printer page
		double pageWidth = pf.getImageableWidth(); //width of printer page

		double scale = pageWidth / panelWidth;
		int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);

		//  make sure not print empty pages
		if (pageIndex >= totalNumPages) {
			response = NO_SUCH_PAGE;
		} else {

			//  shift Graphic to line up with beginning of print-imageable region
			g2.translate(pf.getImageableX(), pf.getImageableY());

			//  shift Graphic to line up with beginning of next page to print
			g2.translate(0f, -pageIndex * pageHeight);

			//  scale the page so the width fits...
			g2.scale(scale, scale);

			componentToBePrinted.paint(g2); //repaint the page for printing

			enableDoubleBuffering(componentToBePrinted);
			response = Printable.PAGE_EXISTS;
		}
		return response;
	}

	public static void disableDoubleBuffering(Component c) {
		RepaintManager currentManager = RepaintManager.currentManager(c);
		currentManager.setDoubleBufferingEnabled(false);
	}

	public static void enableDoubleBuffering(Component c) {
		RepaintManager currentManager = RepaintManager.currentManager(c);
		currentManager.setDoubleBufferingEnabled(true);
	}

}
